import {
  getDeviceDataTypeList,
  getDevicePage,
  getDeviceList,
  getDevicePropertyDataTypeList,
  getDeviceTypeList,
} from '@/api/collection/device'
import { postDictItemList, postDictItemPageList } from '@/api/sys/dict'
import { getAllRoleList, isAccountExist } from '@/api/sys/system'
import { BasicColumn, FormSchema } from '@/components/Table'

export const columns: BasicColumn[] = [
  {
    title: '设备编码',
    dataIndex: 'devicCode',
  },
  {
    title: '设备名称',
    dataIndex: 'deviceName',
  },
  {
    title: '参数名称',
    dataIndex: 'propertyName',
  },
  {
    title: '测量时间',
    dataIndex: 'measureTime',
  },
  {
    title: '测量值',
    dataIndex: 'measureValue',
  },
  {
    title: '单位',
    dataIndex: 'unitName',
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: 'deviceName',
    label: '设备名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: '[startDateTime, endDateTime]',
    // label: '时间',
    component: 'RangePicker',
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      // placeholder: ['开始日期、时间', '结束日期、时间'],
      showTime: { format: 'HH:mm:ss' },
    },
    colProps: { span: 6 },
  },
]

export const accountFormSchema: FormSchema[] = [
  {
    label: '数据分类',
    field: 'type',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'roleValue',
    },
    required: true,
  },
  {
    field: 'name',
    label: '数据名称',
    component: 'Input',
    // helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
      {
        trigger: 'blur',
        validator(_, value) {
          return new Promise((resolve, reject) => {
            if (!value) return resolve()
            isAccountExist(value)
              .then(resolve)
              .catch((err) => {
                reject(err.message || '验证失败')
              })
          })
        },
      },
    ],
  },
  {
    field: 'code',
    label: '设备参数',
    component: 'Input',
    required: true,
    ifShow: false,
  },
  {
    field: 'createtime',
    label: '数据日期',
    component: 'DatePicker',
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      showTime: { format: 'HH:mm:ss' },
    },
  },
  {
    field: 'value',
    label: '参数值',
    component: 'InputNumber',
  },
]

export const dataFormSchema: FormSchema[] = [
  {
    label: '设备类型',
    field: 'deviceTypeId',
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getDeviceTypeList,
        labelField: 'name',
        valueField: 'id',
        onChange: async (e, v) => {
          const ret = await getDeviceList({
            type: v.code,
          })
          const floorOptions = ret.map((item) => ({
            value: item.id,
            label: item.name,
            key: item.id,
          }))
          formModel.deviceId = undefined //  reset deviceId value
          const { updateSchema } = formActionType
          updateSchema({
            field: 'deviceId',
            componentProps: {
              options: floorOptions,
              onChange: async (e, v) => {
                const dtItem = await getDevicePropertyDataTypeList({ deviceId: e })
                const dataTypeItemOptions = dtItem.map((item) => ({
                  value: item.id,
                  label: item.name,
                  key: item.id,
                  dictItemList: item.dictItemList,
                }))
                formModel.dataTypeId = undefined
                const { updateSchema } = formActionType
                updateSchema({
                  field: 'dataTypeId',
                  componentProps: {
                    options: dataTypeItemOptions,
                    onChange: async (key, item) => {
                      const { updateSchema } = formActionType
                      if (item && item.dictItemList.length) {
                        const measureDictItem = item.dictItemList.map((item) => ({
                          value: item.itemId,
                          label: item.itemName,
                          key: item.itemId,
                        }))
                        formModel.measureValue = undefined
                        updateSchema({
                          field: 'measureValue',
                          component: 'Select',
                          componentProps: {
                            options: measureDictItem,
                          },
                        })
                      } else {
                        formModel.measureValue = undefined
                        updateSchema({
                          field: 'measureValue',
                          component: 'Input',
                        })
                      }
                    },
                  },
                })
              },
            },
          })
        },
      }
    },
    required: true,
  },
  {
    field: 'deviceId',
    label: '设备名称',
    component: 'Select',
    componentProps: {
      options: [],
    },
    required: true,
  },
  {
    label: '设备参数',
    field: 'dataTypeId',
    component: 'Select',
    componentProps: {
      options: [],
    },
    required: true,
  },
  {
    field: 'measureDataValueDTOList',
    label: '适用范围',
    slot: 'range',
    required: true,
    defaultValue: [
      {
        deviceTypeId: undefined,
        alarmTypeId: undefined,
      },
    ],
    rules: [
      {
        required: true,
        message: '请配置适用范围',
      },
      {
        validator(_, value) {
          return new Promise((resolve, reject) => {
            if (!value.length) {
              reject('请配置适用范围')
            } else if (value.filter((item) => !item.deviceTypeId || !item.alarmTypeId).length) {
              reject('请完善适用范围')
            } else {
              return resolve()
            }
          })
        },
      },
    ],
  },
  {
    label: '测量时间',
    field: 'measureTime',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
    },
    required: true,
  },
  {
    label: '测量值',
    field: 'measureValue',
    component: 'Input',
    rules: [
      { required: true },
      {
        validator: (rule, value, callback) => {
          const reg = /^[\u4E00-\u9FA5A-Za-z0-9_]+$/
          if (reg.test(value)) {
            callback()
          } else {
            return Promise.reject(rule.message)
          }
        },
        message: '数字、字母、汉字',
      },
    ],
  },
]
